*initial restrictions


clear all
set more off
cap log close
version 13


// global paths 
global datainpath "..."
global dataoutpath "..."
global graphpath "..." 
global outputpath "..."


log using "${outputpath}restriction1.log", replace
use "${datainpath}all_survival.dta", clear

drop if  imm_category!="A" & imm_category!="E" & imm_category!="F" & imm_category!="G"
drop citiz borough coutry_bth imm_category country_bg regstat_fa sosial 
** age restriction, 25-61
drop if age<25
drop if age>61 & age !=.
* drop if missing net_naering (net business income)
drop if net_naering==.
drop if pi_other==.
* drop duplicated unem_benefit variables
drop unemp_ben k_147   
** municipality & county
replace mun= lignkom if (mun=="0000" | mun=="" | mun=="9999" | mun=="8888") & lignkom!="0000" & lignkom!="" & lignkom!="9999" & lignkom!="8888"
drop lign*
* municipality
drop if municipality == "0000"
** gen county, 19 counties, 1 to 20, 13 missing, first two digits of the municipality
gen county=substr(municipality, 1, 2)
destring county,replace
drop if county>20  
drop if county==13 
** education variables 
destring persnum year, replace
sort persnum year
drop if persnum==persnum[_n+1] & year==year[_n+1]
*one ob
merge 1:1 persnum year using ".../edu4digit.dta"
drop if _merge==2 
*fake ids
bysort persnum (year): gen id = 1 if _n==1
replace id = sum(id)
replace id = . if missing(persnum)
bysort famnum (year): gen famid = 1 if _n==1
replace famid = sum(famid)
replace famid = . if missing(famnum) | famnum == 0
gen headfam= famnum==persnum
drop famnum persnum 
order id famid headfam year age
label variable id "personal id"
label variable famid "family id"
label variable headfam "head of the family, id=famid"



save "${dataoutpath}all_survival1.dta", replace

